package com.glqiang.util;

import com.glqiang.security.CustomUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author glq1218
 */
public class SecurityUtils {

    /**
     * 获取登录用户
     * @return 登录用户
     */
    public static CustomUserDetails getLoginUser() {
        return (CustomUserDetails) getAuthentication().getPrincipal();
    }

    public static Authentication getAuthentication() {
        SecurityContext context = SecurityContextHolder.getContext();
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Long getUserId() {
        return getLoginUser().getUser().getId();
    }
}
